#include<bits/stdc++.h>
#define R register int 
using namespace std; 
const int N=1005;
char s[N][100],t[N][100],s1[N][100],s2[N][100];
int a[N],b[N],n;
bool pd(int s){
	int a1=0,a2=0,a3=0;
	for(R i=1; i<=strlen(s2[s]+1); ++i){
		if(s2[s][i]=='.') a1++;
		if((s2[s][i]>'9' || s2[s][i]<'0') && s2[s][i+1]=='0' && s2[s][i+2]>='0' && s2[s][i+2]<='9') return 0;
		if(s2[s][i]==':'){
			if(a1!=3) return 0;
			a2++;
			if(a2>1) return 0;
		}
		if(s2[s][i]!='.' && s2[s][i]!=':' && (s2[s][i]>'9' || s2[s][i]<'0')) return 0;
	}
	if(a2!=1 || a1!=3) return 0;
	a1=a2=0;
	for(R i=1; i<=strlen(s2[s]+1); ++i){
		if(s2[s][i]>='0' && s2[s][i]<='9') a1=a1*10+s2[s][i]-48;
		else{
			a2++;
			if(a2<=4 && a1>255) return 0;
			if(a2==5 && a1>65535) return 0;
			a1=0;
		}
	}
	a2++;
	if(a2<=4 && a1>255) return 0;
	if(a2==5 && a1>65535) return 0;
	a1=0;	
	if(a2!=5) return 0;
	return 1;
}
bool cm(int x,int y){
	if(strlen(s2[x]+1)!=strlen(s2[y]+1)) return 0;
	for(R i=1; i<=strlen(s2[x]+1); ++i){
		if(s2[x][i]!=s2[y][i]) return 0;
	} 
	return 1;
}
int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	scanf("%d",&n);
	for(R i=1; i<=n; ++i){
		scanf("%s",s1[i]+1);
		scanf("%s",s2[i]+1);
		if(pd(i)==0){
			a[i]=123876;
			printf("ERR\n");
			continue;
		}
		if(s1[i][1]=='S'){
			for(R j=1; j<=i-1; ++j){
				if(s1[j][1]!='S' || a[j]!=0) continue;
				if(cm(i,j)==1){
					a[i]=2138764;
					printf("FAIL\n");
					break;
				}
			}
			if(a[i]==0) printf("OK\n");
		}
		else{
			for(R j=1; j<=i-1; ++j){
				if(s1[j][1]=='C' || a[j]!=0) continue;
				if(cm(i,j)==1){
					printf("%d\n",j);
					a[i]=12375;
					break;
				} 
			} 
			if(a[i]==0) printf("FAIL\n");
		}
	}
	return 0;
}
